package com.qq.BFMRSE.controllerHelper;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import com.qq.BFMRSE.model.BloomFilter;
import com.qq.BFMRSE.model.indexTree;
import com.qq.BFMRSE.model.indexTreeNode;

public class newIndexHelper {public static void updateLeafBit( List<indexTreeNode>leafNodes, Map<String,List<Integer>> bitSetInfoMap)
{
	Iterator<indexTreeNode>iterator=leafNodes.iterator();
	while(iterator.hasNext())
	{
		indexTreeNode indexNode=iterator.next();
		String tempFileID=indexNode.getFID();
		List<Integer> bitList=bitSetInfoMap.get(tempFileID);
		BloomFilter bf=new BloomFilter();
		for(int i=0;i<bitList.size();i++)
		{
			bf.getBits().set(bitList.get(i));
		}
		indexNode.setBf(bf);
	}
}
public static void updateInteralNodeBit(indexTree tree)
{
	Stack< indexTreeNode>stack=new Stack<indexTreeNode>();
	indexTreeNode currNode=tree.getRoot();
	indexTreeNode preNode=null;
	while(!stack.isEmpty()||currNode!=null)
	{
		while(currNode!=null)
		{
			stack.push(currNode);
			currNode=currNode.getLeft();
		}
		currNode=stack.peek();
		if(currNode.getRight()==null||currNode.getRight()==preNode)
		{
			if(currNode.getLeft()!=null&&currNode.getRight()!=null)
			{
				indexTreeNode node1=currNode.getLeft();
				indexTreeNode node2=currNode.getRight();
				BloomFilter bf=new BloomFilter();
				int length=bf.BitsLength();
				for(int i=0;i<length;i++)
				{
					if(node1.getBf().getBits().get(i)==true||node2.getBf().getBits().get(i)==true)
					{
						bf.getBits().set(i);
					}
				}
				currNode.setBf(bf);
			}
			preNode=currNode;
			stack.pop();
			currNode=null;
		}
		else {
			currNode=currNode.getRight();
		}	
	}	
}
}
